﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.DirectoryServices;
using System.DirectoryServices.AccountManagement;
using AccountManagement.Entities;

namespace AccountManagement.Utility
{
    public class Helper
    {
        public static String GetStringValue(SearchResult result, String propertyName)
        {
            if (result.Properties.Contains(propertyName))
            {
                return (String)result.Properties[propertyName][0];
            }
            else
            {
                return String.Empty;
            }
        }

        public static Group ConvertToGroup(GroupPrincipal groupPrincipal)
        {
            Group newGroup = new Group();
            newGroup.Name = String.IsNullOrEmpty(groupPrincipal.Name) ? "" : groupPrincipal.Name;
            newGroup.Description = String.IsNullOrEmpty(groupPrincipal.Description) ? "" : groupPrincipal.Description;
            newGroup.SamAccountName = String.IsNullOrEmpty(groupPrincipal.SamAccountName) ? "" : groupPrincipal.SamAccountName;
            //newGroup.GroupType = groupPrincipal.GroupScope.ToString();
            return newGroup;
        }

        public static long GetInt64Value(SearchResult result, string attribute)
        {
            if (result != null)
            {
                if (result.Properties.Contains(attribute))
                {
                    return (Int64)result.Properties[attribute][0];
                }
            }

            return -1;
        }

        public static bool IsActive(SearchResult result, String attribute)
        {

            int flags = (int)result.Properties[attribute][0];

            /*if (!Convert.ToBoolean(flags & 0x0002)) 
                return true; 
            else 
                return false;*/

            return !Convert.ToBoolean(flags & 0x0002);
        }
    }
}